﻿function AT2_SavedPanelUI_Setup()
	AT2_UI_SavedMessages:SetBackdropColor(0, 0, 0, 0.85)
	AT2_UI_SavedMessages_isQueuedText:SetText("Sorba rakva")
--		AT2_UIButtonAutoSize(AT2_UI_SavedMessages_List)

	AT2_UI_ResizeHandler()
--[[  
	
	StaticPopupDialogs["AT2_NewSavedMessage"] = {
	text = "Add meg a mentendö üzenet \124cffFFFF00nevét\124r.",
	button1 = "Ok",
	button2 = "Mégse",
	whileDead = true,
	hideOnEscape = true,
	timeout = 0,
	enterClicksFirstButton = true,
	hasEditBox = true,
	
	EditBoxOnTextChanged = function (self, data)
		if not(self:GetText() == "") then
			local foundMessageWithSameName=false
			for i=1,tcount(AT2_Save["Messages"]) do
				if AT2_Save["Messages"][i]["title"]==self:GetText() then
					foundMessageWithSameName=true
					AT2_Print("foundMessageWithSameName="..tostring(foundMessageWithSameName).." text: "..self:GetText().." - currentsav: "..select(1, strsplit("\a",AT2_Save["Messages"][i])),"debug","new saved message staticpopup")
				else
					AT2_Print("foundMessageWithSameName="..tostring(foundMessageWithSameName).." text: "..self:GetText().." - currentsav: "..select(1, strsplit("\a",AT2_Save["Messages"][i])),"debug","new saved message staticpopup")
				end
			end
			if (foundMessageWithSameName==true) then
				self:GetParent().button1:Disable()
			else
				self:GetParent().button1:Enable()
			end
		else
			self:GetParent().button1:Disable()
		end
		
	end,
	OnAccept = function (self, data) 
		AT2_WriteSavedMessage("create",tcount(AT2_Save["Messages"])+1,"--nincsadat--",self.editBox:GetText())
		AT2_EnableButton(AT2_UI_SavedMessages_List)
		AT2_EnableButton(AT2_UI_SavedMessages_Delete)
		AT2_EnableButton(AT2_UI_SavedMessages_Insert)
		AT2_SelectedSavedMessage=tostring(tcount(AT2_Save["Messages"]))
		AT2_UI_SavedMessages_Name:SetText(AT2_ReadSavedMessage("name",AT2_SelectedSavedMessage))
		AT2_UI_SavedMessages_EditBox:SetText("")
		AT2_UI_SavedMessages_EditBox:SetFocus()
		
	end,
	OnShow = function (self)
		self.button1:Disable()
	end,
	}
	
	]]--
	function AT2_ReadSavedMessage(type, which) 
		if (type=="q") then
			return AT2_Save["Messages"][tostring(which)]["queue"]
		elseif (type=="name" or type=="title") then
			return AT2_Save["Messages"][tostring(which)]["title"]
		elseif (type=="msg") then
			return AT2_Save["Messages"][tostring(which)]["text"]
		elseif (type=="all") then
			return AT2_Save["Messages"][tostring(which)]["queue"],AT2_Save["Messages"][tostring(which)]["title"],AT2_Save["Messages"][tostring(which)]["text"]
		else
			AT2_Print("Mentett üzenet olvasására érvénytelen típus lett kiválasztva. Részletek: T"..type.." W"..which,"error" )
		end
	end
	
	function AT2_WriteSavedMessage(type, which, text, title,queue)
	if (type==nil) then type="nil" end if which==nil then which="nil" end if text==nil then text="nil" end if title==nil then title="nil" end if queue==nil then queue=false end
	AT2_Print("T"..type..", W"..which..", T"..text..", T2"..title,"debug","AT2_WriteSavedMessage")
		if not(which == 0) then
			if not(AT2_Save["Messages"][tostring(which)]) then AT2_Save["Messages"][tostring(which)]={} end
			if (type=="q") then
				AT2_Save["Messages"][tostring(which)]["queue"]=queue
			elseif (type=="name") then
				AT2_Save["Messages"][tostring(which)]["title"]=title
			elseif (type=="msg") then
				AT2_Save["Messages"][tostring(which)]["text"]=text
			elseif (type=="create") then
				AT2_Save["Messages"][tostring(which)]["text"]=text;
				AT2_Save["Messages"][tostring(which)]["title"]=title;
				AT2_Save["Messages"][tostring(which)]["queue"]=false;
			else
				AT2_Print("Mentett üzenet írására érvénytelen típus lett kiválasztva. Részletek: T"..type.." W"..which.." T"..text,"error" )
			end
		else
			AT2_Print("Az összes mentett üzenet törölve lett? A 0. mentett üzenetre próbált írni.","debug","AT2_WriteSavedMessage, Type"..type..", Which"..which..", Text"..text)
		end
	end
	
	if not(AT2_ModuleList) then
		AT2_ModuleList = {}
	end
	tinsert(AT2_ModuleList,"SavedPanel");
end


function AT2_UI_SavedMessages_Eventhandler(name, event)
	local e=event;local c="click";local efg="editfocusgained";local efl="editfocuslost";local tc="textchanged";local md="mousedown";local mu="mouseup";local me="mouseenter";local ml="mouseleave";local r="resize";local s="show";u="update"
	local nname=name:GetName()
	if nname=="AT2_UI_SavedMessages_EditBox" and e==efg then
		AT2_UI_EmptyEditBox(name,AT2_MessageEditBoxHelp, "focus")
	elseif nname=="AT2_UI_SavedMessages_EditBox" and e==efl then
		AT2_UI_EmptyEditBox(name,AT2_MessageEditBoxHelp, "focuslost")
	elseif nname=="AT2_UI_SavedMessages_EditBox" and e==tc then
		AT2_UI_SavedMessages_CharLeft:SetText(strsub(AT2_RedYellowGreenNum(AT2_UI_SavedMessages_EditBox:GetNumLetters(),AT2_UI_SavedMessages_EditBox),1).."/250 karakter")
		if (AT2_SelectedSavedMessage==nil) then
			AT2_UI_EmptyEditBox(name,AT2_SavedMessageEditBoxHelp,"focuslost")
		else
			AT2_Save["Messages"][AT2_SelectedSavedMessage]=AT2_UI_SavedMessages_EditBox:GetText()
		end
	end
end